package controller

import (
	"context"
	"sync"

	"github.com/gin-gonic/gin"

	"easygo/pkg/redis"
	"easygo/pkg/wrapper"
)

var (
	TestInstance *Test
	TestOnce     sync.Once
)

func GetTestInstance() *Test {
	TestOnce.Do(func() {
		TestInstance = &Test{}
	})
	return TestInstance
}

//
type Test struct {
}

//jwt登录状态测试
func (t *Test) JwtTest(c *gin.Context) {

	username := c.GetString("username")

	wrapper.ResSuccess(c, username)
}

//redis存储测试
func (t *Test) RedisSetTest(c *gin.Context) {
	username := "xiao"
	token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJePxk"
	err := redis.GetRedis(context.Background()).Set(username, token, 0).Err()
	if err != nil {
		wrapper.ResError(c, wrapper.ErrRedisSet)
		return
	}
	wrapper.ResSuccess(c, nil)
}

//redis读取测试
func (t *Test) RedisGetTest(c *gin.Context) {
	val, err := redis.GetRedis(context.Background()).Get("xiao").Result()
	if err != nil {
		wrapper.ResError(c, wrapper.ErrRedisGet)
		return
	}
	wrapper.ResSuccess(c, val)
}
